package com.example.aircraftwar2024.activity;

import com.example.aircraftwar2024.aircraft.HeroAircraft;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.aircraftwar2024.game.BaseGame;
import com.example.aircraftwar2024.game.EasyGame;
import com.example.aircraftwar2024.game.HardGame;
import com.example.aircraftwar2024.game.MediumGame;
import com.example.aircraftwar2024.activity.OfflineActivity.GameMode;

import java.util.ArrayList;
import java.util.Date;


public class GameActivity extends AppCompatActivity {
    private static final String TAG = "GameActivity";
    private int gameMode = 0;
    public static int screenWidth,screenHeight;
    private Mhandler mHandler;
    public static boolean gameOverFlg;
    private int score;
    private boolean music;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ActivityManager.getActivityManager().addActivity(this);
        super.onCreate(savedInstanceState);
        mHandler = new Mhandler();
        getScreenHW();
        /*TODO:根据用户选择的难度加载相应的游戏界面*/
        BaseGame[] gameModes = new BaseGame[]{new EasyGame(this), new MediumGame(this), new HardGame(this)};
        if(getIntent() != null){
            gameMode = getIntent().getIntExtra("gameType",0);
            music = getIntent().getBooleanExtra("music", true);
        }
        gameOverFlg = false;
        BaseGame baseGameView = gameModes[gameMode];
        baseGameView.setMusic(music);
        setContentView(baseGameView);
        new Thread() {
            @Override
            public void run() {
                while(true) {
                    if (gameOverFlg) {
                        System.out.println("test");
                        Message msg = Message.obtain();
                        msg.what = 1; // 消息标识
                        msg.obj = "A"; // 消息内存存放
                        mHandler.sendMessage(msg);
                        score = baseGameView.getScore();
                        break;
                    }
                }
                HeroAircraft.reset();
            }
        }.start();

    }

    public void getScreenHW(){
        //定义DisplayMetrics 对象
        DisplayMetrics dm = new DisplayMetrics();
        //取得窗口属性
        getDisplay().getRealMetrics(dm);

        //窗口的宽度
        screenWidth= dm.widthPixels;
        //窗口高度
        screenHeight = dm.heightPixels;

        Log.i(TAG, "screenWidth : " + screenWidth + " screenHeight : " + screenHeight);
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    class Mhandler extends Handler {

        // 通过复写handlerMessage() 从而确定更新UI的操作
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    Intent intent = new Intent(GameActivity.this, RankActivity.class);
                    intent.putExtra("gameMode",gameMode);
                    intent.putExtra("score",score);
                    intent.putExtra("username","test");
                    intent.putExtra("time",new Date().getTime());
                    intent.putExtra("music", music);
                    startActivity(intent);
                    break;
            }
        }
    }

    public int getMode(){return gameMode;}
}